/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package bd2;

import java.util.ArrayList;
import java.util.Date;
import java.text.DateFormat;
import java.text.SimpleDateFormat;

/**
 *
 * @author tolkjen
 */
public class DataStore {
    private static DataStore instance = new DataStore();
    
    /* Temporary data holder - to be eliminated */
    private ArrayList<Object[]> kursyTable, wyklTable, katTable, kursanciTable,
            osobyTable;
    
    public static DataStore getInstance() {
        return instance;
    }
    
    /* Opens a connection with database. Needs to be called before any other
     database operation. */
    public Boolean openConnection() {
        DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT);
        
        try {
            kursyTable = new ArrayList<Object[]>();
            kursyTable.add(new Object[]{0, "B", "Rommel Erwin", 
                df.parse("01.01.12"), df.parse("01.02.12"), 200, 7, 0, 1});
            kursyTable.add(new Object[]{1, "B", "Goring Hermann", 
                df.parse("01.01.12"), df.parse("12.01.12"), 200, 15, 1, 1});
        } catch (Exception e) {}
        
        wyklTable = new ArrayList<Object[]>();
        wyklTable.add(new Object[]{"123/213", "Rommel", "Erwin", "123", "afrikakorps@ss.com"});
        wyklTable.add(new Object[]{"456/345", "Goring", "Hermann", "4546", "luftwaffe@ss.com"});
        
        kursanciTable = new ArrayList<Object[]>();
        kursanciTable.add(new Object[]{0, "Groźny", "Ivan", "123", "@o2.pl", 
            new Double(10.5), false});
        kursanciTable.add(new Object[]{1, "Wielki", "Alexander", "345345", "mail.com", 
            new Double(1000), true});
        
        osobyTable = new ArrayList<Object[]>();
        osobyTable.add(new Object[]{0, "Rzymski", "Neron", "546456", "@o2.pl"});
        osobyTable.add(new Object[]{1, "Rzymski", "Kaligula", "774", "a@rome.com"});
        
        katTable = new ArrayList<Object[]>();
        katTable.add(new Object[]{0, "A"});
        katTable.add(new Object[]{1, "B"});
        katTable.add(new Object[]{2, "C"});
        
        return true;
    }
    
    /* Closes previously opened connection */
    public Boolean closeConnection() {
        return true;
    }            
    
    /** Array format:
     * Kurs id (Integer)
     * Kategoria name (String)
     * Name and second name of Wykladowca (String)
     * Start date (Date)
     * Finish date (Date)
     * Kurs cost (Double)
     * Number of Kursanci attending (Integer)
     * 
     * @return Container holding arrays of Object or null on error.
     */
    public ArrayList<Object[]> getKursy() {
        return kursyTable;
    }
    
    /** Array format:
     * Kategoria name (String)
     * Kategoria id (Integer)
     * Name and second name of Wykladowca (String)
     * Wykladowca nr upr (String)
     * Start date (Date)
     * Finish date (Date)
     * Kurs cost (Double)
     * 
     * @param idKurs Kurs id.
     * @return Container holding arrays of Object or null on error.
     */
    public Object[] getKurs(int idKurs) {
        return new Object[]{"B", 1, "Rommel Erwin", "xx/yy", new Date(), new Date(), 200};
    }
    
    /** Adds a Kurs to database
     * 
     * @param katId Kategoria id
     * @param tutorNr Wykladowca powers nr
     * @param start Start date of Kurs
     * @param finish Last date of Kurs
     * @param cost Kurs cost
     * @return true if operation succeeded, false if not.
     */
    public Boolean addKurs(Integer katId, String tutorNr, Date start, 
            Date finish, Double cost) {
        kursyTable.add(new Object[]{999, "TODO!", "TODO!", 
            start, finish, cost, 0});
        return true;
    }
    
    /** Update Kurs information
     * 
     * @param kursId Kurs ID
     * @param katId Kategoria id
     * @param tutorNr Wykladowca powers nr
     * @param start Start date of Kurs
     * @param finish Last date of Kurs
     * @param cost Kurs cost
     * @return true if operation succeeded, false if not.
     */
    public Boolean updateKurs(Integer kursId, Integer katId, String tutorNr, 
            Date start, Date finish, Double cost) {
        // implement this!
        return true;
    }
    
    /** Remove Kurs from database
     * 
     * @param idKurs Kurs id
     * @return true if operation succeeded, false if not.
     */
    public Boolean deleteKurs(Integer idKurs) {
        for (int i=0; i<kursyTable.size(); i++) {
            if ((Integer) kursyTable.get(i)[0] == idKurs) {
                kursyTable.remove(i);
                return true;
            }
        }
        return false;
    }
    
    /** Information about people attending a Kurs. Return array format:
     * Osoba id (Integer)
     * Second name (String)
     * Name (String)
     * Telephone number (String)
     * Email address (String)
     * Total payment (Double)
     * Total payment >= Kurs cost (Boolean)
     * 
     * @param idKurs Kurs id
     * @return Container holding arrays of Object or null on error.
     */
    public ArrayList<Object[]> getKursanci(int idKurs) {
        return kursanciTable;
    }
    
    /** Information about people available. Array format:
     * Osoba id (Integer)
     * Second name (String)
     * Name (String)
     * Telephone number (String)
     * Email address (String)
     * 
     * @return Container holding arrays of Object or null on error.
     */
    public ArrayList<Object[]> getOsoby() {
        return osobyTable;
    }
    
    /** Adds an Osoba to a Kurs.
     * 
     * @param idKurs Kurs id
     * @param idOsoby Osoba id
     * @return true if operation succeeded, false if not.
     */
    public Boolean addOsobaToKurs(int idKurs, int idOsoby) {
        kursanciTable.add(new Object[]{idOsoby, "???", "???", "???", "???", 
            new Double(0.0), false});
        return true;
    }
    
    /** Removes an Osoba from a Kurs.
     * 
     * @param idKurs Kurs id
     * @param idOsoby Osoba id
     * @return true if operation succeeded, false if not.
     */
    public Boolean removeOsobaFromKurs(int idKurs, int idOsoby) {
        // implement this!
        return true;
    }
    
    /** Adds a payment to an Osoba on a Kurs.
     * 
     * @param idKurs Kurs id
     * @param idOsoba Osoba id
     * @param money Money paid
     * @return true if operation succeeded, false if not.
     */
    public Boolean addWplata(int idKurs, int idOsoba, Double money) {
        for (int i=0; i<kursanciTable.size(); i++) {
            if ((Integer) kursanciTable.get(i)[0] == idOsoba) {
                Double amount = (Double) kursanciTable.get(i)[5];
                kursanciTable.get(i)[5] = new Double(amount + money);
                return true;
            }
        }
        return false;
    }
    
    /** Information about Wykladowcy with a given Kategoria. Array format:
     * Wykladowca nr upr (String)
     * Second name (String)
     * Name (String)
     * Telephone number (String)
     * Email address (String)
     * 
     * @return Container holding arrays of Object or null on error.
     */
    public ArrayList<Object[]> getWykladowcy(int kategoria) {
        return wyklTable;
    }
    
    /** Information about Kategorie. Array format:
     * Kategoria id (Integer)
     * Name (String)
     * 
     * @return Container holding arrays of Object or null on error.
     */
    public ArrayList<Object[]> getKategorie() {
        return katTable;
    }
}
